/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bibsys;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Pardal
 */
public class BibSys {

    private static MainWindow mainWindow;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        /* Set look and feel */
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(BibSys.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(BibSys.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(BibSys.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(BibSys.class.getName()).log(Level.SEVERE, null, ex);
        }
        mainWindow = new MainWindow();

    }

    public static void showWindow() {
        if (mainWindow != null) {
            mainWindow.setVisible(true);
        }
    }

    //TODO: Separar os métodos do parser numa classe "Utils".
    //TODO: Testar parser com os três tipo de delimitador de tag.
    //TODO: Função para gerar texto a partir das entidades "Publicação" 
    
    
    public static void parseTag(Publicacao p, String tagLine) {
        List<String> tag = new ArrayList<>();
        try { // Try to parse a '{' delimited tag.
            Pattern curlyBracesTag = Pattern.compile("\\s*(.*)=\\s*\\{(.*)\\}");            
            Matcher tagMatcher = curlyBracesTag.matcher(tagLine);
            if (tagMatcher.find()) {
                p.addTag(tagMatcher.group(1), tagMatcher.group(2));            
                return;
            } else {
                throw new NoSuchFieldException();
            }                    
        } catch (NoSuchFieldException e){
            System.out.println("Not a curlyBrace Tag!");            
        } catch (Exception e ){
            System.out.println(e.toString());
        }
        
        try { // Try to parse a '"' delimited tag.
            Pattern quotationMarkTag = Pattern.compile("\\s*(.*)=\\s*\"(.*)\"");            
            Matcher tagMatcher = quotationMarkTag.matcher(tagLine);
            if (tagMatcher.find()) {
                p.addTag(tagMatcher.group(1), tagMatcher.group(2));            
                return;
            } else {
                throw new NoSuchFieldException();
            }                    
        } catch (NoSuchFieldException e){
            System.out.println("Not a curlyBrace Tag!");            
        } catch (Exception e ){
            System.out.println(e.toString());
        }
        
        try {
            Pattern noMarkTag = Pattern.compile("\\s*(.*)=\\s*(.*)");
            Matcher tagMatcher = noMarkTag.matcher(tagLine);
            if (tagMatcher.find()){
                p.addTag(tagMatcher.group(1), tagMatcher.group(2));                                   
            } else {
                throw new NoSuchFieldException();
            }
        } catch (NoSuchFieldException e){
            System.out.println("Not a tag!");
        }
    }

public static List<Publicacao> pasrseBib(String bibString){
        List<Publicacao> publicacoes = new ArrayList<>();
        bibString = bibString.substring(bibString.indexOf("@"));
        Scanner scan = new Scanner(bibString);
        scan.useDelimiter("@");        
        while (scan.hasNext()){ //Enquanto houverem publicações 
            String buffer = scan.next();
            Publicacao p = new Publicacao();
            p.setTipo(buffer.substring(0,buffer.indexOf('{')));
            p.setNome(buffer.substring(buffer.indexOf('{')+1,buffer.indexOf(',')));                                  
            Scanner tagScan = new Scanner(buffer); 
            tagScan.nextLine(); //Skips type/name line.
            while (tagScan.hasNextLine()){
                BibSys.parseTag(p,tagScan.nextLine());
            }            
            publicacoes.add(p);
        }                        
        return publicacoes;
    }
}